package com.red.wood.module.eval.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.eval.dao.DictionaryDao;

@Repository
public class DictionaryDaoImpl implements DictionaryDao{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 根据type 获取所有对应的字典数据
	 * @param type
	 * @return
	 */
	public List<Map<String, Object>> getByType(String type){
		if(StringUtils.isNotBlank(type)){
			String sql = "select ID as id, CODE_TYPE as codeType, CODE_KEY as codeKey, CODE_VALUE as codeValue, DESCRIPTION as description from t_eval_dictionary where CODE_TYPE = " + "'"+type+"'";
			return jdbcTemplate.queryForList(sql);
		}
		return null;
	}
	
	/**
	 * 根据类型和key获取字典数据
	 * @param type
	 * @param key
	 * @return
	 */
	public Map<String, Object> getByTypeAndKey(String type, String key){
		if(StringUtils.isNotBlank(key) && StringUtils.isNotBlank(type)){
			String sql = "select ID as id, CODE_TYPE as codeType, CODE_KEY as codeKey, CODE_VALUE as codeValue, DESCRIPTION as description from t_eval_dictionary where CODE_TYPE = " +  "'"+type+"'" + "and CODE_KEY=" +  "'"+key+"'";
			List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
			if(dataList != null && dataList.size() > 0){
				return dataList.get(0);
			}
		}
		return null;
	}
	
	/**
	 * 根据类型和key值获取 value 
	 * @param type
	 * @param key
	 * @return
	 */
	public String getValueByTypeAndKey(String type, String key){
		if(StringUtils.isNotBlank(key) && StringUtils.isNotBlank(type)){
			String sql = "select CODE_VALUE as codeValue from t_eval_dictionary where CODE_TYPE = " +  "'"+type+"'" + "and CODE_KEY=" +  "'"+key+"'";
			List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
			if(dataList != null && dataList.size() > 0){
				return CommonUtils.valueOf(dataList.get(0).get("codeValue"));
			}
		}
		return null;
	}

}
